package com.mmall.common;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by root on 3/21/18.
 */
public class TokenCache {
    public static Logger log = LoggerFactory.getLogger(TokenCache.class);

    public static final  String TOKEN_PREFIX = "token_";

    private static LoadingCache<String, String> loadCache = CacheBuilder.newBuilder()
            .initialCapacity(1000)
            .maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
            .build(new CacheLoader<String, String>() {
                @Override
                public String load(String s) throws Exception {
                    return null;
                }
            });

    public static void set(String key, String token){
        loadCache.put(key,token);
    }

    public static String get(String key){
        try {
            String token = loadCache.get(key);
            if (token == null){
                return "";
            }
        } catch (ExecutionException e) {
            log.error("get token error",e);
        }
        return "";
    }
}
